export default {
    namespaced: true,
    state: {
        //token值
        token: ""
    },
    getters: {

    },
    mutations: {
        setToken(state, payload) {
            state.token = payload.value
        }
    },
    actions: {
        async fetchToken({ commit }) {
            //获取用户信息
            const [err, res] = await uni.getUserProfile({
                desc: "用来给用户提供更好的服务",
            });
            const { encryptedData, rawData, iv, signature } = res;

            //获取临时登录凭据
            const [err1, res1] = await uni.login();
            const { code } = res1;

            //  向后端来获取 token
            const res2 = await uni.$u.http.post("/users/wxlogin", {
                encryptedData,
                rawData,
                iv,
                signature,
                code,
            });
            const { token } = res2;
            commit('setToken', { value: token })
        }
    }
}